package org.oims.simpleserver.endpoint;

import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class EndpointPool {
    private final Map<String, Endpoint> idEndpointMap = new ConcurrentHashMap<>();

    public void put(String id, Endpoint endpoint){
        Endpoint ep = idEndpointMap.get(id);
        if(ep != null){
            ep.setId(null);
        }
        endpoint.setId(id);
        idEndpointMap.put(id, endpoint);
    }

    public Endpoint get(String id){
        return idEndpointMap.get(id);
    }

    public void remove(String id, Endpoint endpoint){
        if(endpoint != null){
            Endpoint ep = idEndpointMap.get(id);
            if(ep == endpoint){
                idEndpointMap.remove(id);
            }
        }else {
            idEndpointMap.remove(id);
        }
    }
}
